package m.jclecai.com.activity;

import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import m.jclecai.com.R;
import m.jclecai.com.adapter.GuideAdapter;
import m.jclecai.com.base.BaseActivity;
import m.jclecai.com.utils.PreferenceUtil;
import m.jclecai.com.utils.ScreenUtil;

/**
 * 引导页
 */
public class GuideActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.viewpager)
    ViewPager vp;

    @BindView(R.id.mTvJump)
    TextView mTvJump;

    @BindView(R.id.mRAlIndicatorContainer)
    LinearLayout mRAlIndicatorContainer;

    private List<View> views;
    private GuideAdapter vpAdapter;
    private int openCount = 0;

    @Override
    protected int getStatusBarColor() {
        return R.color.color_a3e9fd;
    }

    @Override
    protected int getContentView() {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);// 设置全屏
        return R.layout.activity_guide;
    }

    //引导图片资源（后面加一个用来侧滑关闭guide）
    private static final int[] mGuides = {R.mipmap.app_icon_1, R.mipmap.app_icon_2,
            R.mipmap.app_icon_3,R.mipmap.app_icon_4,R.mipmap.app_icon_5,R.color.color_a3e9fd};

    @Override
    protected void initView() {
        super.initView();
        if(!PreferenceUtil.isShowGuideFirst(getBaseContext())){
            startActivity(new Intent(getBaseContext(),MainActivity.class));
            finish();
            return;
        }
        views = new ArrayList<>();
        LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);

        for (int mGuide : mGuides) {
            ImageView iv = new ImageView(this);
            iv.setFitsSystemWindows(true);
            iv.setScaleType(ImageView.ScaleType.FIT_XY);
            iv.setLayoutParams(mParams);
            iv.setImageResource(mGuide);
            views.add(iv);
        }

        //初始化Adapter
        vpAdapter = new GuideAdapter(this,views);
        vp.setAdapter(vpAdapter);
        createIndicator(0);
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if(positionOffset > 0.5 && position == mGuides.length -2 && openCount == 0){
                    openCount = 1;
                    PreferenceUtil.setShowGuideFirst(getBaseContext(),false);
                    startActivity(new Intent(getBaseContext(),MainActivity.class));
                    finish();
                }
            }

            @Override
            public void onPageSelected(int position) {
                mRAlIndicatorContainer.removeAllViews();
                createIndicator(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @OnClick({R.id.mTvJump,R.id.tv_continue})
    void jump() {
        PreferenceUtil.setShowGuideFirst(getBaseContext(),false);
        startActivity(new Intent(getBaseContext(),MainActivity.class));
        finish();
    }

    @Override
    public void onClick(View v) {
        jump();
    }

    void createIndicator(int index) {
        for (int i = 0; i < mGuides.length -1; i++) {
            ImageView imageView = new ImageView(this);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ScreenUtil.dpToPx(this, 5), ScreenUtil.dpToPx(this, 5));
            layoutParams.leftMargin = ScreenUtil.dpToPx(this, 5);
            imageView.setLayoutParams(layoutParams);
            if (i == index)
                imageView.setImageResource(R.drawable.txt_count_bg);
            else
                imageView.setImageResource(R.drawable.txt_index_bg);
            mRAlIndicatorContainer.addView(imageView);
        }

        mTvJump.setVisibility(index == mGuides.length - 2 ? View.VISIBLE : View.GONE);
        mRAlIndicatorContainer.setVisibility(index == mGuides.length - 1 ? View.GONE : View.VISIBLE);
    }

}
